package me.twoday.library.mine.common;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

public class SharedPreferenceUtil 
{
	private static final String TAG = "SharedPreferenceUtil";
	private static SharedPreferenceUtil mSPUtil = new SharedPreferenceUtil();
	private Context mContext;
	
	/**
	 * 실제 사용할 객체를 얻는다.
	 * 
	 * @param context
	 * 			실행 컨텍스트 
	 * @return SharedPreferenceUtil
	 * 			사용할 인스턴스
	 */
	public static SharedPreferenceUtil getInstance(Context context)
	{
		mSPUtil.mContext = context;
		return mSPUtil;
	}
	
	/**
	 * String형 데이터를 저장하기 위한 함수 
	 * 
	 * @param key 키  
	 * @param value 값
	 */
	public void putSharedPreference(String key, String value)
	{
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString(key, value);
		editor.commit();
	}
	
	/**
	 * int형 데이터를 저장하기 위한 함수 
	 * 
	 * @param key 키  
	 * @param value 값
	 */
	public void putSharedPreference(String key, int value)
	{
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putInt(key, value);
		editor.commit();
	}
	
	/**
	 * Boolean형 데이터를 저장하기 위한 함수 
	 * 
	 * @param key 키  
	 * @param value 값
	 */
	public void putSharedPreference(String key, Boolean value)
	{
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putBoolean(key, value);
		editor.commit();
	}
	
	/**
	 * long형 데이터를 저장하기 위한 함수 
	 * 
	 * @param key 키  
	 * @param value 값
	 */
	public void putSharedPreference(String key, long value)
	{
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putLong(key, value);
		editor.commit();
	}
	
	/**
	 * 키에 맞는 값을 가져옴 
	 * 
	 * @param key 키 
	 * @return String 있을경우 값, 없을경우 null 반환 
	 */
	public String getStringSharedPreference(String key)
	{
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
		return prefs.getString(key, null);
	}

	/**
	 * 키에 맞는 값을 가져옴 
	 * 
	 * @param key 키 
	 * @return Boolean 있을경우 값, 없을경우 false 반환 
	 */
	public Boolean getBooleanSharedPreference(String key)
	{
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
		return prefs.getBoolean(key, false);
	}
	
	/**
	 * 키에 맞는 값을 가져옴 
	 * 
	 * @param key 키 
	 * @return int 있을경우 값, 없을경우 0 반환 
	 */
	public int getIntSharedPreference(String key)
	{
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
		return prefs.getInt(key, 0);
	}
	
	/**
	 * 키에 맞는 값을 가져옴 
	 * 
	 * @param key 키 
	 * @return long 있을경우 값, 없을경우 0 반환 
	 */
	public long getLongSharedPreference(String key)
	{
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
		return prefs.getLong(key, 0);
	}
	
	
	public Boolean hasPreference(String key)
	{
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
		return prefs.contains(key);
	}
	
	public void removePreference(String key)
	{
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
		SharedPreferences.Editor editor = prefs.edit();
		
		if(hasPreference(key))
		{
			editor.remove(key);
			Log.e(TAG, "Success remove data: " + key);
		}
		else
		{
			Log.e(TAG, "Fail remove data: "+ key);
		}
	}
}
